package com.green.card;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.widget.RadioGroup;

import com.baidu.mobstat.StatService;
import com.green.card.adapter.MyFragmentAdapter;
import com.green.card.base.BaseActivity;
import com.green.card.fragment.HomeFragment;
import com.green.card.fragment.MineFragment;
import com.green.card.utils.PackegeIdUtils;
import com.green.card.utils.ToastUtils;
import com.green.card.widget.NoScrollViewPager;
import com.zhuoyian.pluginsdk.ZYASDKUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity{
    //记录用户首次点击返回键的时间
    private long firstTime = 0;

    /**
     * 作为页面容器的ViewPager
     */
    private NoScrollViewPager mPager;
    /**
     * 页面集合
     */
    private List<Fragment>    fragmentList;
    // 首页Fragment
    private HomeFragment      mHomeFragment;
    // 我的Fragment
    private MineFragment      mMineFragment;
    // RadioGroup
    private RadioGroup mGroup;

    //后台配置的推送值
    private String pindKey = "7DB15785AC157B62777FFB427DF890EE";
    private String p;

    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        mPager = (NoScrollViewPager) findViewById(R.id.vp_content);
        mGroup = (RadioGroup) findViewById(R.id.main_rg);
    }

    @Override
    protected void setListener() {
    }


    @Override
    public void initSdk() {
        p = PackegeIdUtils.getRawFileContent(this, R.raw.p);
        // 百度统计:建议此代码埋点在统计路径触发的第一个页面中，若可能存在多个则建议都埋点
        baiduStart();
        //启动自家sdk
        initZYASdk();
    }

    private void baiduStart() {
        //设置渠道号
        StatService.setAppChannel(this,p,true);
        StatService.start(this);
    }


    /**
     * 自家sdk启动
     */
    private void initZYASdk() {
        int packId = Integer.parseInt(p);
        //初始化自家sdk
        ZYASDKUtil.init(this,packId,pindKey,false);
    }

    @Override
    public void initData() {
        // 勾选默认为首页
        mGroup.check(R.id.main_rb_home);

        fragmentList = new ArrayList<>();
        mHomeFragment = new HomeFragment();
        mMineFragment = new MineFragment();
        fragmentList.add(mHomeFragment);
        fragmentList.add(mMineFragment);
        mPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),fragmentList));

        // 监听RadioGroup的选择事件
        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.main_rb_home:
                        changeView(0);
                        break;
                    case R.id.main_rb_mine:
                        changeView(1);
                        break;

                    default:
                        break;
                }
            }
        });

    }


    //手动设置ViewPager要显示的视图
    private void changeView(int desTab) {
        mPager.setCurrentItem(desTab, true);
    }


    /**
     * 按返回键两下退出应用
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                ToastUtils.toast(this,"再按一次退出程序");
                firstTime = secondTime;
                return true;
            } else {
                System.exit(0);
            }
        }

        return super.onKeyUp(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ZYASDKUtil.destroy(this);
    }
}
